<template>
  <div class="home-new" ref="newGoodsRef">
    <HomePanel title="新鲜好物" subTitle="新鲜出炉 品质靠谱">
      <!-- 顶部的右边插槽 -->
      <template #right>
        <xtx-more></xtx-more>
      </template>
      <Transition name="fade">
        <!-- 默认插槽 -->
        <ul class="goods-list" v-if="newList && newList.length">
          <li v-for="item in  newList" :key="item.id">
            <RouterLink :to="`/product/${item.id}`">
              <img :src="item.picture" alt="">
              <p class="name ellipsis">{{ item.name }}</p>
              <p class="price">&yen;{{ item.price }}</p>
            </RouterLink>
          </li>
        </ul>
        <!-- 骨架屏组件 -->
        <HomeSkeleton v-else bg="#f0f9f4"></HomeSkeleton>
      </Transition>
    </HomePanel>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { findNew } from '@/api/home'
import HomePanel from '../com/home-panel.vue'
import HomeSkeleton from '../com/home-skeleton.vue'
// 导入数据懒加载函数
import { useLazyData } from '@/hook'
// 定义数据
const newList = ref([])
// 要监听的dom对象
const newGoodsRef = ref(null)
useLazyData(newGoodsRef, () => {
  findNew().then(({ result }) => {
    newList.value = result
  })
})

</script>

<style lang="less" scoped>
.fade {
  &-leave {
    &-active {
      // 两个动画同事触发，导致布局出现问题
      position: absolute; //骨架离开动画在上一层的触发
      width: 100%;
      transition: opacity .5s .2s;
      z-index: 1;
    }

    &-to {
      opacity: 0;
    }
  }
}

.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;

  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    // 使用mixinLess中的函数
    .hoverShadow();

    img {
      width: 306px;
      height: 306px;
    }

    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
    }

    .price {
      color: @priceColor;
    }
  }
}
</style>